package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.VO.CartVo;
import com.example.demo.model.Customer;
import com.example.demo.model.QueryPage;
import com.example.demo.service.CustomerService;
import com.example.demo.service.impl.CustomerServiceimpl;
import com.example.demo.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController{
    /*getMapping=URL+ method=get  即路径加get方法*/

    @Resource
    private CustomerService customerService;

    @GetMapping("/index1")
    public String index(){
        return "Product";
    }

    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @GetMapping("/register")
    public String register(){
        return "register";
    }

    @GetMapping("/download")
    public String download(){
        return "download";
    }

    @GetMapping("/fileupload")
    public String fileupload(){
        return "fileupload";
    }

    
    @GetMapping("/mana")
    public  String customer(Model model){
        List<Customer> list=customerService.list(null);
        model.addAttribute("manaList",list);
        System.out.println(list.toString());
        return "customer";
    }

    @GetMapping("/editCustomer")
    public  String editCustomer(int id,Model model){

       Customer customer= customerService.getById(id);
       model.addAttribute("customer",customer);

       return "customerEdit";

    }

    @GetMapping("/deleteCustomer")
    public  String deleteCustomer(int id){
        customerService.removeById(id);
        return "redirect:/customer/mana";
    }

    @PostMapping("/saveCustomer")
    public  String saveCustomer(Customer customer,Model model){
        customerService.updateById(customer);
        return "redirect:/customer/mana";
    }

    @GetMapping("/addCustomer")
    public String addCustomer(){
        return "customerAdd";
    }

    @PostMapping("/saveAddCustomer")
    @ResponseBody
    public  Boolean saveAddCustomer(Customer customer){

        customerService.saveOrUpdate(customer);
        return true;
    }

    @GetMapping("/address")
    @ResponseBody
    public JsonResult<List<Customer>> findaddress(HttpSession session) {
        List<Customer> data = customerService.findAddress(getUidFromSession(session));
        return new JsonResult<>(OK, data);
    }

    @PostMapping("/get_by_uid")
    @ResponseBody
    public JsonResult<Customer> getByUid(HttpSession session) throws CustomerServiceimpl.UserNotFoundException {
        Customer data = customerService.getByUid(getUidFromSession(session));
        return new JsonResult<>(OK, data);
    }

    @PostMapping("/change_info")
    @ResponseBody
    public JsonResult<Void> changeInfo(Customer customer, HttpSession session) throws CustomerServiceimpl.UserNotFoundException, CustomerServiceimpl.UpdateException {

        Integer uid = getUidFromSession(session);
        customerService.changeInfo(uid,customer);
        return new JsonResult<>(OK);
    }



    /*分页*/
    @GetMapping("/page")
    @ResponseBody
    public Object getPage(QueryPage queryPage){
        System.out.println(queryPage.toString());
        Page page=new Page(queryPage.getCurPage(),queryPage.getPageSize());
        Page<Customer> page1=(Page<Customer>)customerService.page(page);
        return page1;
    }

    /*@GetMapping("/page")
    @ResponseBody
    public Object getPage1(QueryPage queryPage){
        System.out.println(queryPage.toString());
        Page page=new Page(queryPage.getCurPage(),queryPage.getPageSize());

        Page<Customer> page1=(Page<Customer>)customerService.pageQueryCustomer(page,queryPage.getName(),queryPage.getSno());
        return page1;
    }*/

    @GetMapping("/pageview")
    public ModelAndView getView(){
        return new ModelAndView("customerList");
    }

    @GetMapping("/find/{id}")
    @ResponseBody
    public Object findOne(@PathVariable("id")int id){
        Customer customer=customerService.getById(id);
        return customer;
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Boolean deletecustomer(@PathVariable("id") int id){
        customerService.removeById(id);
        return  true;
    }

}
